package mx.ipn.presentacion.nomina;

import java.util.GregorianCalendar;

import com.trolltech.qt.gui.QDialog;

import mx.ipn.presentacion.ConexionConNegocios;
import mx.ipn.presentacion.nomina.ui.*;
import mx.ipn.to.*;
import com.trolltech.qt.gui.QTableWidgetItem;
public class Modificar extends QDialog{
	Ui_Modificar mod;
	QTableWidgetItem newItem = null;
	QTableWidgetItem itemid = null;
	DescuentoTO [] arrdesc;
	PercepcionTO [] arrper;
	public DescuentoTO desc;
	public PercepcionTO per;
	public Modificar(){
		mod = new Ui_Modificar();
		mod.setupUi(this);
		mod.tableWidget.setRowCount(1);
		mod.tableWidget.itemClicked.connect(this,"selectPD()");
		mod.pushButton.clicked.connect(this, "buscar()");
		mod.buttonBox.accepted.connect(this, "accept()");
        mod.buttonBox.rejected.connect(this, "reject()");
		//this.show();
	}
    public Modificar(QDialog parent){
		
	}
	public DescuentoTO  buscaDesc(int id){
		DescuentoTO des=null;
		for(int i=0;i<arrdesc.length;i++)
		{
			if(arrdesc[i].getIdDescuento()==id)
			   des=arrdesc[i];
		}
		return des;
	}
	public PercepcionTO buscarPer(int id){
		PercepcionTO per=null;
		for(int i=0;i<arrper.length;i++)
		{
			if(arrper[i].getIdPercepcion()==id)
			   per=arrper[i];
		}
		return per;
	}
	public void selectPD(){
		QTableWidgetItem newItem=null;
		int fila = mod.tableWidget.currentRow();
		itemid = mod.tableWidget.item(fila,0);
		newItem = mod.tableWidget.item(fila,4);
		if(newItem.text().compareToIgnoreCase("descuento")==0)
		desc=  buscaDesc(Integer.parseInt(itemid.text()));
		if(newItem.text().compareToIgnoreCase("percepcion")==0)
		per= buscarPer(Integer.parseInt(itemid.text()));
	}
	public void buscar(){
		int desclen=0;
		int perlen=0;
		try
		{
			arrdesc = (DescuentoTO[])ConexionConNegocios.invocaServicio("selectDescuento",DescuentoTO[].class); 
			arrper =  (PercepcionTO[])ConexionConNegocios.invocaServicio("selectPercepcion",PercepcionTO[].class);
			mod.tableWidget.clearContents();
			if(arrdesc!=null)
			desclen = arrdesc.length;
			if(arrper!=null)
			perlen = arrper.length;
			mod.tableWidget.setRowCount(desclen+perlen);
			if(arrdesc!=null){
		       for(int i=0;i<arrdesc.length;i++)
		        	mod.tableWidget.insertRow(i);
		       for(int j=0;j<arrdesc.length;j++){
		       	newItem = new QTableWidgetItem(String.valueOf(arrdesc[j].getIdDescuento()));
	    	    mod.tableWidget.setItem(j,0,newItem);
	    	    newItem = new QTableWidgetItem(String.valueOf(arrdesc[j].getTipoPercepcionDescuentoTO().getNombre()));
	    	    mod.tableWidget.setItem(j,1,newItem);
	    	    newItem = new QTableWidgetItem(String.valueOf(arrdesc[j].getNombre()));
	    	    mod.tableWidget.setItem(j,2,newItem);
	    	    newItem = new QTableWidgetItem(String.valueOf(arrdesc[j].getDescripcion()));
	    	    mod.tableWidget.setItem(j,3,newItem);
	    	    newItem = new QTableWidgetItem("descuento");
	    	    mod.tableWidget.setItem(j,4,newItem);
		       }
		    }
		    if(arrper!=null){
		       for(int i=0;i<arrper.length;i++)
		        	mod.tableWidget.insertRow(i);
		       for(int j=0;j<arrper.length;j++){
		       	newItem = new QTableWidgetItem(String.valueOf(arrper[j].getIdPercepcion()));
	    	    mod.tableWidget.setItem(j,0,newItem);
	    	    newItem = new QTableWidgetItem(String.valueOf(arrper[j].getTipoPercepcionDescuentoTO().getNombre()));
	    	    mod.tableWidget.setItem(j,1,newItem);
	    	    newItem = new QTableWidgetItem(String.valueOf(arrper[j].getNombre()));
	    	    mod.tableWidget.setItem(j,2,newItem);
	    	    newItem = new QTableWidgetItem(String.valueOf(arrper[j].getDescripcion()));
	    	    mod.tableWidget.setItem(j,3,newItem);
	    	    newItem = new QTableWidgetItem("percepcion");
	    	    mod.tableWidget.setItem(j,4,newItem);
		       }
		    }
	    }catch(Exception e){}
	}
}